package com.appsindy.common.action;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.appsindy.common.json.JSONResultCommon;
import com.appsindy.common.util.DateUtils;
import com.appsindy.reserv.ReservConstants;
import com.appsindy.reserv.bo.Combo;
import com.appsindy.reserv.service.ReservationServices;
import com.appsindy.reserv.vo.CalendarMatrix;
import com.appsindy.reserv.vo.GroupInfoVO;
import com.appsindy.reserv.vo.Info;
import com.appsindy.reserv.vo.ReserveInfoRoomVO;
import com.appsindy.reserv.vo.ReserveInfoTypeVO;
import com.opensymphony.xwork2.Action;


public class ReserveOverviewAction extends AppsindyBaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private static final String FWD_RESERVE_POPUP 		= "reserveByPopup";
	private static final String FWD_GROUP_RESULT_POPUP 	= "groupResultPopup";
	private static final String FWD_RESERVE_INFO 		= "toReserveInfo";	
	private static final String FWD_RESERVE_INFO_ROOM 	= "toReserveInfoRoom";	
	
	private List<Combo> monthList;
	private List<Combo> yearList;
	private List<Combo> groupList;
	private List<Combo> customerList;
	private List<Combo> roomList;
	private List<Combo> statusList;
	private List<Combo> groupPopupList;

	private Date dateFromReserv;
	private Date dateToReserv;
	private int monthReserv;
	private String monthReservLabel;
	private int yearReserv;
	private int groupReserv;
	private String groupStatusReserv;
	private int customerReserv;
	private String customerStatusReserv;
	private int roomReserv;
	private String roomStatusReserv;
	private int groupPopup;
	private Date dateFromPopup;
	private Date dateToPopup;
	private int serviceHeaderIdPopup;
	private int groupResultPopup;
	private String groupResultNamePopup;
	private String groupResultStatusPopup;
	
	private CalendarMatrix matrix;	
	private List<Info> groupResultList;
	private List<Info> customerResultList;
	private List<Info> roomResultList;
	private List<Info> groupResultPopupList;
	
	private ReservationServices reservationServices;
	
	
	private void clearParameter(){
		monthReserv 		= 0;
		monthReservLabel 	= null;
		yearReserv 			= 0;
		groupReserv 		= 0;
		groupStatusReserv 	= null;
		customerReserv 		= 0;
		customerStatusReserv= null;
	}
	public String execute() {
		log.info("Init Reservation Overview ...");

		clearParameter();
		
		Calendar c = Calendar.getInstance(Locale.US);
		int month 	= (c.get(Calendar.MONTH)+1);
		int year 	= c.get(Calendar.YEAR);
		setMonthReserv(month);
		setYearReserv(year);
		
		return initialOverview();
	}
	
	private String initialOverview(){
		log.info("Init Reservation Overview : Combo list ...");
		String[] monthLabel = {"", "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
		
		setMonthList(getReservationServices().getMonthCombos());
		setYearList(getReservationServices().getYearCombos());
		setGroupList(getReservationServices().getGroupCombos());
		setCustomerList(getReservationServices().getCustomerCombos());
		setRoomList(getReservationServices().getRoomCombos());
		setStatusList(getReservationServices().getStatusCombos());
		
		setMatrix(getReservationServices().getCurrentReserve(getMonthReserv(), getYearReserv()));		
		setGroupResultList(getReservationServices().selectGroup(getGroupReserv(), getGroupStatusReserv()));
		setCustomerResultList(getReservationServices().selectCustomer(getCustomerReserv(), getCustomerStatusReserv()));
		setRoomResultList(getReservationServices().selectRoom(getRoomReserv(), getRoomStatusReserv()));
		
		setMonthReservLabel(monthLabel[getMonthReserv()]);
		
		return SUCCESS;
	}
    
	public String searchCalendar() {
		log.info("Search Calendar Reservation Overview ...");
		
		return initialOverview();
	}

	public String searchGroup() {
		log.info("Search Group Reservation Overview ...");
		
		return initialOverview();
	}

	public String searchCustomer() {
		log.info("Search Customer Reservation Overview ...");
		
		return initialOverview();
	}

	public String searchRoom() {
		log.info("Search Room Reservation Overview ...");
		
		return initialOverview();
	}
	
    @SuppressWarnings("unchecked")
	public String toReserveByPopup(){
        // Debug
        if(this.log.isDebugEnabled()) {
            this.log.debug("// Method toReserveByPopup // START ================================================= ");
        }        
        try{
        	List<Combo> groupPopL = (List<Combo>)getContextAttribute("CTX_GROUP_POPUP_LIST");
        	if(groupPopL == null || groupPopL.size() < 1){
        		groupPopL = getReservationServices().getGroupPopupCombos();
        		setContextAttribute("CTX_GROUP_POPUP_LIST", groupPopL);
        	}
    		setGroupPopupList(groupPopL);
    		
    		if(getDateToPopup() == null){
    			setDateToPopup(DateUtils.addDate(getDateFromPopup(), 7));
    		}            
        } catch(Exception ex){
            addActionError(ex.getMessage());
            ex.printStackTrace();
        }        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toReserveByPopup // END ================================================= ");
        }        
        return FWD_RESERVE_POPUP;
    }
    
    public String toGroupResultPopup() {        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toGroupResultPopup // START ================================================= ");
        }        
        try {
    		setGroupResultPopupList(getReservationServices().selectGroupResultPopup(getGroupResultPopup(), getGroupResultStatusPopup()));
           
        } catch (Exception ex) {
            addActionError(ex.getMessage());
            ex.printStackTrace();
        }        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toGroupResultPopup // END ================================================= ");
        }        
        return FWD_GROUP_RESULT_POPUP;
    }    
    public String toCustomerResultPopup() {        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toCustomerResultPopup // START ================================================= ");
        }        
        try {
    		setGroupResultPopupList(getReservationServices().selectCustomerResultPopup(getGroupResultPopup(), getGroupResultStatusPopup()));
           
        } catch (Exception ex) {
            addActionError(ex.getMessage());
            ex.printStackTrace();
        }        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toCustomerResultPopup // END ================================================= ");
        }        
        return FWD_GROUP_RESULT_POPUP;
    }    
    public String toRoomResultPopup() {        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toRoomResultPopup // START ================================================= ");
        }        
        try {
    		setGroupResultPopupList(getReservationServices().selectRoomResultPopup(getGroupResultPopup(), getGroupResultStatusPopup()));
           
        } catch (Exception ex) {
            addActionError(ex.getMessage());
            ex.printStackTrace();
        }        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toRoomResultPopup // END ================================================= ");
        }        
        return FWD_GROUP_RESULT_POPUP;
    }        
	@SuppressWarnings("finally")
	public String checkGroupJSON(){
    	log.info("checkGroupJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't check group");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	List<Info> result = getReservationServices().selectGroupResultPopup(getGroupResultPopup(), getGroupResultStatusPopup());
        	jsonResultCommon = new JSONResultCommon(Action.SUCCESS, result);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }    
	
	@SuppressWarnings("finally")
	public String checkCustomerJSON(){
    	log.info("checkGroupJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't check customer");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	List<Info> result = getReservationServices().selectCustomerResultPopup(getGroupResultPopup(), getGroupResultStatusPopup());
        	jsonResultCommon = new JSONResultCommon(Action.SUCCESS, result);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }    	
	@SuppressWarnings("finally")
	public String checkRoomJSON(){
    	log.info("checkRoomJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't check room");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	List<Info> result = getReservationServices().selectRoomResultPopup(getGroupResultPopup(), getGroupResultStatusPopup());
        	jsonResultCommon = new JSONResultCommon(Action.SUCCESS, result);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }    	
    
    @SuppressWarnings("unchecked")
	public String toReserveInfoRoom() {
        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toReserveInfoRoom // START ================================================= ");
        }
        
        try {
            ReserveInfoRoomVO vo = new ReserveInfoRoomVO();
            vo.setServiceHeaderId(getServiceHeaderIdPopup());
            GroupInfoVO groupInfoVO = new GroupInfoVO();
            groupInfoVO.setGroupId(groupResultPopup);
            vo.setGroupInfoVO(groupInfoVO);
            
            getSession().put(ReservConstants.S_RESERVE, vo);
        } catch (Exception ex) {
            addActionError(ex.getMessage());
            ex.printStackTrace();
        }
        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toReserveInfo // END ================================================= ");
        }
        
        return FWD_RESERVE_INFO_ROOM;
    }    
    
    @SuppressWarnings("unchecked")
	public String toReserveInfo() {
        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toReserveInfo // START ================================================= ");
        }
        
        try {
            ReserveInfoTypeVO vo = new ReserveInfoTypeVO();
            vo.setDateFrom(dateFromPopup);
            vo.setDateTo(dateToPopup);
            GroupInfoVO groupInfoVO = new GroupInfoVO();
            groupInfoVO.setGroupId(groupPopup);
            vo.setGroupInfoVO(groupInfoVO);
            
            getSession().put(ReservConstants.S_RESERVE, vo);
        } catch (Exception ex) {
            addActionError(ex.getMessage());
            ex.printStackTrace();
        }
        
        // Debug
        if (this.log.isDebugEnabled()) {
            this.log.debug("// Method toReserveInfo // END ================================================= ");
        }
        
        return FWD_RESERVE_INFO;
    }    
    
	public void setReservationServices(ReservationServices reservationServices) {
		this.reservationServices = reservationServices;
	}

	public ReservationServices getReservationServices() {
		return reservationServices;
	}

	public void setMonthReserv(int monthReserv) {
		this.monthReserv = monthReserv;
	}

	public int getMonthReserv() {
		return monthReserv;
	}

	public void setYearReserv(int yearReserv) {
		this.yearReserv = yearReserv;
	}

	public int getYearReserv() {
		return yearReserv;
	}

	public void setMatrix(CalendarMatrix matrix) {
		this.matrix = matrix;
	}

	public CalendarMatrix getMatrix() {
		return matrix;
	}

	public List<Combo> getMonthList() {
		return monthList;
	}

	public void setMonthList(List<Combo> monthList) {
		this.monthList = monthList;
	}

	public List<Combo> getYearList() {
		return yearList;
	}

	public void setYearList(List<Combo> yearList) {
		this.yearList = yearList;
	}

	public List<Combo> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Combo> groupList) {
		this.groupList = groupList;
	}

	public List<Combo> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Combo> customerList) {
		this.customerList = customerList;
	}

	public List<Combo> getStatusList() {
		return statusList;
	}

	public void setStatusList(List<Combo> statusList) {
		this.statusList = statusList;
	}

	public List<Info> getGroupResultList() {
		return groupResultList;
	}

	public void setGroupResultList(List<Info> groupResultList) {
		this.groupResultList = groupResultList;
	}

	public List<Info> getCustomerResultList() {
		return customerResultList;
	}

	public void setCustomerResultList(List<Info> customerResultList) {
		this.customerResultList = customerResultList;
	}

	public int getGroupReserv() {
		return groupReserv;
	}

	public void setGroupReserv(int groupReserv) {
		this.groupReserv = groupReserv;
	}

	public String getGroupStatusReserv() {
		return groupStatusReserv;
	}

	public void setGroupStatusReserv(String groupStatusReserv) {
		this.groupStatusReserv = groupStatusReserv;
	}

	public int getCustomerReserv() {
		return customerReserv;
	}

	public void setCustomerReserv(int customerReserv) {
		this.customerReserv = customerReserv;
	}

	public String getCustomerStatusReserv() {
		return customerStatusReserv;
	}

	public void setCustomerStatusReserv(String customerStatusReserv) {
		this.customerStatusReserv = customerStatusReserv;
	}

	public String getMonthReservLabel() {
		return monthReservLabel;
	}

	public void setMonthReservLabel(String monthReservLabel) {
		this.monthReservLabel = monthReservLabel;
	}
	public List<Combo> getGroupPopupList() {
		return groupPopupList;
	}
	public void setGroupPopupList(List<Combo> groupPopupList) {
		this.groupPopupList = groupPopupList;
	}
	public int getGroupPopup() {
		return groupPopup;
	}
	public void setGroupPopup(int groupPopup) {
		this.groupPopup = groupPopup;
	}
	public Date getDateFromReserv() {
		return dateFromReserv;
	}
	public void setDateFromReserv(Date dateFromReserv) {
		this.dateFromReserv = dateFromReserv;
	}
	public Date getDateToReserv() {
		return dateToReserv;
	}
	public void setDateToReserv(Date dateToReserv) {
		this.dateToReserv = dateToReserv;
	}
	public Date getDateFromPopup() {
		return dateFromPopup;
	}
	public void setDateFromPopup(Date dateFromPopup) {
		this.dateFromPopup = dateFromPopup;
	}
	public Date getDateToPopup() {
		return dateToPopup;
	}
	public void setDateToPopup(Date dateToPopup) {
		this.dateToPopup = dateToPopup;
	}
	public int getGroupResultPopup() {
		return groupResultPopup;
	}
	public void setGroupResultPopup(int groupResultPopup) {
		this.groupResultPopup = groupResultPopup;
	}
	public String getGroupResultStatusPopup() {
		return groupResultStatusPopup;
	}
	public void setGroupResultStatusPopup(String groupResultStatusPopup) {
		this.groupResultStatusPopup = groupResultStatusPopup;
	}
	public List<Info> getGroupResultPopupList() {
		return groupResultPopupList;
	}
	public void setGroupResultPopupList(List<Info> groupResultPopupList) {
		this.groupResultPopupList = groupResultPopupList;
	}
	public int getServiceHeaderIdPopup() {
		return serviceHeaderIdPopup;
	}
	public void setServiceHeaderIdPopup(int serviceHeaderIdPopup) {
		this.serviceHeaderIdPopup = serviceHeaderIdPopup;
	}
	public String getGroupResultNamePopup() {
		return groupResultNamePopup;
	}
	public void setGroupResultNamePopup(String groupResultNamePopup) {
		this.groupResultNamePopup = groupResultNamePopup;
	}
	public List<Info> getRoomResultList() {
		return roomResultList;
	}
	public void setRoomResultList(List<Info> roomResultList) {
		this.roomResultList = roomResultList;
	}
	public List<Combo> getRoomList() {
		return roomList;
	}
	public void setRoomList(List<Combo> roomList) {
		this.roomList = roomList;
	}
	public String getRoomStatusReserv() {
		return roomStatusReserv;
	}
	public void setRoomStatusReserv(String roomStatusReserv) {
		this.roomStatusReserv = roomStatusReserv;
	}
	public int getRoomReserv() {
		return roomReserv;
	}
	public void setRoomReserv(int roomReserv) {
		this.roomReserv = roomReserv;
	}
    
}
